package thread;

import java.util.concurrent.Semaphore;

/*
 * @author zzr
 * @date: 2025/04/11  16:06
 * @description:
 */
public class ThreadDemo47 {
    public static void main(String[] args) throws InterruptedException {

        // 设置信号量的许可数量为 3
        Semaphore semaphore = new Semaphore(3);

        // 尝试获取 3 次许可
        semaphore.acquire();
        System.out.println("P 操作 1 ");
        Thread.sleep(1000);
        semaphore.acquire();
        System.out.println("P 操作 2 ");
        Thread.sleep(1000);
        semaphore.acquire();
        System.out.println("P 操作 3 ");

        Thread.sleep(1000);
        // 释放 3 次许可
        semaphore.release();
        semaphore.release();
        semaphore.release();

        System.out.println("所有许可已经释放~~~");
    }
}
